<?php

namespace p5\objects;

/**
 * Created by PhpStorm.
 * User: simon
 * Date: 2015/11/24
 * Time: 8:51
 */
class StringUtils
{
    static public function shorten($string, $len)
    {
        if (mb_strlen($string) > $len) {
            return self::substr($string, 0, $len) . " ... ";
        }
        return $string;
    }

    static public function underline_case($name)
    {
        $temp_array = array();
        for ($i = 0; $i < strlen($name); $i++) {
            $ascii_code = ord($name[$i]);
            if ($ascii_code >= 65 && $ascii_code <= 90) {
                if ($i == 0) {
                    $temp_array[] = chr($ascii_code + 32);
                } else {
                    $temp_array[] = '_' . chr($ascii_code + 32);
                }
            } else {
                $temp_array[] = $name[$i];
            }
        }
        return implode('', $temp_array);
    }

    public static function camel_case($string)
    {
        $value = ucwords(str_replace(['-', '_'], ' ', $string));
        return lcfirst(str_replace(" ", "", $value));
    }

    public static function substr($string, $start, $length = null)
    {
        return mb_substr($string, $start, $length, 'UTF-8');
    }

    public static function start_with($str, $needle)
    {
        return strpos($str, $needle) === 0;
    }

    //第一个是原串,第二个是 部份串
    public static function end_with($haystack, $needle)
    {
        $length = strlen($needle);
        if ($length == 0) {
            return true;
        }
        return (substr($haystack, -$length) === $needle);
    }

    public static function lpad($string, $chr, $len)
    {
        return self::pad($string, $chr, $len, true);
    }

    public static function rpad($string, $chr, $len)
    {
        return self::pad($string, $chr, $len, false);
    }

    public static function pad($string, $chr, $len, $left = true)
    {
        while (true) {
            if (strlen($string) >= $len) {
                break;
            }
            if ($left) {
                $string = $chr . $string;
            } else {
                $string = $string . $chr;
            }
        }
        return $string;
    }

    static public function explodeByLen($string, $len)
    {
        $matrix = [];
        $temp = $string;
        while (true) {
            $size = mb_strlen($temp);
            if ($size > $len) {
                $s = mb_substr($temp, 0, $len);
                array_push($matrix, $s);
                $temp = mb_substr($temp, $len);
            } else {
                array_push($matrix, $temp);
                break;
            }
        }
        return $matrix;
    }

    static public function random_string($len)
    {
        $i = 0;
        $str = "";
        while ($i < $len) {
            $str .= chr((rand() % 26) + 97);
            $i++;
        }
        return $str;
    }

    static public function random_nums($len)
    {
        $matrix = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        $i = 0;
        $str = "";
        while ($i < $len) {
            $index = rand(0, 9);
            $str .= $matrix[$index];
            $i++;
        }
        return $str;
    }

    /**
     * 从两个字符串中查找前面的相同部分
     * @param $string1
     * @param $string2
     * @return string
     */
    static public function fetchSamePrefix($string1, $string2)
    {
        $index = 0;
        $size1 = mb_strlen($string1);
        $size2 = mb_strlen($string2);
        $len = min($size1, $size2);
        for ($i = 0; $i < $len; $i++) {
            $temp1 = mb_substr($string1, $i, 1);
            $temp2 = mb_substr($string2, $i, 1);
            if ($temp1 != $temp2) {
                break;
            }
            $index = $i + 1;
        }
        if ($index == 0) {
            return "";
        }
        return mb_substr($string1, 0, $index);
    }
}